<template>
  <el-affix :offset="100">
    <div style="width: 50px;margin-left: -20px;">
      <div class="left-button">
        <el-button size="default" v-if="buttonState.edit" type="primary" @click="editKnow" title="编辑" plain icon="Edit"
          circle />
      </div>
      <div class="left-button">
        <el-button size="default" v-if="buttonState.delete" type="danger" @click="delKnow" title="删除" plain
          icon="Delete" circle />
      </div>
    </div>
  </el-affix>
</template>
<script setup lang="ts">
import { onMounted, reactive } from 'vue';
import farm2Request from '@/service/remoteRequests/Farm2Request';
import notice from '@/components/msg/FarmNotice';
import { RequestTypeEnum } from '@/types/commons/RequestTypeEnum';
import type { DataResponse } from '@/types/commons/DataResponse';
import { useUserInfoStore } from '@/store/useUserInfoStore';
import type { User } from '@/types/system/user';
import farmRoutUtils from '@/hook/farmRoutUtils';
import { ElMessageBox } from 'element-plus';
const useUserInfo = useUserInfoStore();
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
const props = defineProps({
  knowid: {
    type: String,
    required: true
  }
});
const buttonState = reactive({
  edit: false,
  delete: false
});

const editKnow = () => {
  farmRoutUtils.openPageByPathId('/knowEdit', props.knowid, '_self');
}

const delKnow = () => {
  ElMessageBox.confirm('确认删除?', { icon: 'QuestionFilled', type: 'error' })
    .then(() => {
      farm2Request.submit('api/wknow', RequestTypeEnum.delete, null, { id: props.knowid }).then(() => {
        farmRoutUtils.goToHome();
      }).catch((msg: string) => {
        notice.byError(msg);//报错
      });
    }).catch(() => { });
}

const loadUserPops = () => {
  useUserInfo.getUser().then((user: User) => {
    if (user.loginname) {
      farm2Request.submit('api/wknow/pop', RequestTypeEnum.post, null, { id: props.knowid }).then((response: DataResponse) => {
        if ((response.data as [string]).includes('DELETE')) {
          buttonState.delete = true;
        }
        if ((response.data as [string]).includes('EDIT')) {
          buttonState.edit = true;
        }
      }).catch((msg: string) => {
        notice.byError(msg);//报错
      });
    }
  });
}

onMounted(() => {
  loadUserPops();
})
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
</script>
<style scoped>
/* 样式可以根据需要添加 */
/* 确保占满父容器的宽度 */
.left-button {
  margin-top: 20px;
}
</style>
